package com.m7788.util;


import com.m7788.util.MoveDistanceGetRunnable.OnMoveListener;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;

//类似手机滑盖动画工具类
public class UpAndDownAnim {
	private View mAnimView;
	private View mMesureView;
	private int maxH, minH;
	private Interpolator mInterpolator = new DecelerateInterpolator();
	private int mAnimTime = 500;
	private MoveDistanceGetRunnable mDistanceGetRunnable;
	private OnMoveListener mOnMoveListener;
	private boolean isMaxHeight = false;
	private OnAnimFinishedListener mOnAnimFinishedListener;

	public UpAndDownAnim(View mAnimView, View mMesureView, int minH) {
		super();
		this.mAnimView = mAnimView;
		this.mMesureView = mMesureView;
		this.minH = minH;
		init();
	}

	public UpAndDownAnim(View mAnimView, int maxH, int minH) {
		super();
		this.mAnimView = mAnimView;
		this.maxH = maxH;
		this.minH = minH;
		initUI();
	}

	private void initUI() {
		ViewGroup.LayoutParams params = mAnimView.getLayoutParams();
		if (isMaxHeight) {
			params.height = maxH;
		} else {
			params.height = minH;
		}
		mAnimView.setLayoutParams(params);
		mOnMoveListener = new OnMoveListener() {

			@Override
			public void onMoveFinished(int triggerId) {
				if (mOnAnimFinishedListener != null) {
					mOnAnimFinishedListener.OnAnimFinished(triggerId);
				}
			}

			@Override
			public void getCurLoc(float curLoc) {
				ViewGroup.LayoutParams mParams = mAnimView.getLayoutParams();
				mParams.height = (int) curLoc;
				mAnimView.setLayoutParams(mParams);
			}
		};
	}

	private void init() {
		mMesureView.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						maxH = mMesureView.getHeight() + minH;
						if (maxH != minH) {
							mMesureView.getViewTreeObserver()
									.removeGlobalOnLayoutListener(this);
						}
						ViewGroup.LayoutParams params = mAnimView
								.getLayoutParams();
						params.height = minH;
						if (isMaxHeight) {
							params.height = maxH;
						}
						mAnimView.setLayoutParams(params);
					}
				});
		mOnMoveListener = new OnMoveListener() {

			@Override
			public void onMoveFinished(int triggerId) {
				if (mOnAnimFinishedListener != null) {
					mOnAnimFinishedListener.OnAnimFinished(triggerId);
				}
			}

			@Override
			public void getCurLoc(float curLoc) {
				ViewGroup.LayoutParams mParams = mAnimView.getLayoutParams();
				mParams.height = (int) curLoc;
				mAnimView.setLayoutParams(mParams);
			}
		};
	}

	public void setIsMaxHeight(boolean isMax) {
		this.isMaxHeight = isMax;
		if (maxH != 0) {
			if (mDistanceGetRunnable != null) {
				mDistanceGetRunnable.stopAnim();
			}
			ViewGroup.LayoutParams params = mAnimView.getLayoutParams();
			params.height = maxH;
			mAnimView.setLayoutParams(params);
		}
	}

	public void startUpAnim(int triggerId) {
		if (mDistanceGetRunnable != null) {
			mDistanceGetRunnable.stopAnim();
		}
		mDistanceGetRunnable = new MoveDistanceGetRunnable(maxH, minH,
				mAnimTime, mInterpolator, mAnimView, mOnMoveListener, triggerId);
		mAnimView.post(mDistanceGetRunnable);
	}

	public void startDownAnim(int triggerId) {
		if (mDistanceGetRunnable != null) {
			mDistanceGetRunnable.stopAnim();
		}
		mDistanceGetRunnable = new MoveDistanceGetRunnable(minH, maxH,
				mAnimTime, mInterpolator, mAnimView, mOnMoveListener, triggerId);
		mAnimView.post(mDistanceGetRunnable);
	}

	public void setmOnAnimFinishedListener(
			OnAnimFinishedListener onAnimFinishedListener) {
		this.mOnAnimFinishedListener = onAnimFinishedListener;
	}

	public interface OnAnimFinishedListener {
		public void OnAnimFinished(int triggerId);
	}

	public void setAnimTime(int animTime) {
		this.mAnimTime = animTime;
	}
}
